package com.jindi.appuser.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author: wuwei
 * @date: 2018/3/21 17:01
 */

public class MyTimeUtils {

    public static void main(String[] args) {
        System.out.println("20分钟之后：" + new Date(getAfterMinuteTime(20)));
        System.out.println("6个月之后：" + new Date(getAfterMonthTime(6)));
        System.out.println("20年之后：" + new Date(getAfterYearTime(20)));
        System.out.println("yizhou之后：" + new Date(getAfterWeekTime(2)));
    }

    public static long getLiveTime(Date time, String appId) {
        if (time == null) return 0L;
        long now = time.getTime();
        long timeToLive = 50 * 5 * 60 * 1000L;
        switch (appId) {
            case "1":
            case "6":
                //保留五十期，每隔5分钟一期，23:59:59-09:00:00之间不开
                if (now >= getEndTime(23, 59, 59) - timeToLive) {
                    timeToLive += 9 * 60 * 60 * 1000;
                }
                break;
            case "2":
            case "5":
                timeToLive = 100 * 5 * 60 * 1000;
                if (now >= getEndTime(26, 0, 0) - timeToLive) {
                    timeToLive += 9 * 60 * 60 * 1000;
                }
                break;
            case "3":
                if (now >= getEndTime(28, 0, 0) - timeToLive) {
                    timeToLive += 9 * 60 * 60 * 1000;
                }
                break;
            case "4":
                if (now >= getEndTime(22, 0, 0) - timeToLive) {
                    timeToLive += 9 * 60 * 60 * 1000;
                }
                break;
            case "7":
                timeToLive += 9 * 60 * 60 * 1000;
                break;
            case "8":
                timeToLive += 9 * 60 * 60 * 1000;
                break;
            case "9":
                timeToLive += 9 * 60 * 60 * 1000;
                break;
            case "10":
                timeToLive += 9 * 60 * 60 * 1000;
                break;
        }
        return timeToLive;
    }

    /**
     * 返回某段时间之后的时间
     *
     * @return
     */
    public static long getEndTime(int hour, int minute, int second) {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), hour, minute, second);
        return cal.getTimeInMillis();
    }

    /**
     * 返回N分钟之后的时间
     *
     * @return
     */
    public static long getAfterMinuteTime(int minute) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MINUTE, minute);
        return cal.getTimeInMillis();
    }

    /**
     * 返回N个周之后的时间
     *
     * @return
     */
    public static long getAfterWeekTime(int week) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.WEEK_OF_MONTH, week);
        return cal.getTimeInMillis();
    }

    /**
     * 返回N个月之后的时间
     *
     * @return
     */
    public static long getAfterMonthTime(int month) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, month);
        return cal.getTimeInMillis();
    }

    /**
     * 返回N天之后的时间
     *
     * @return
     */
    public static long getAfterDayTime(int year) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, year);
        return cal.getTimeInMillis();
    }

    /**
     * 返回N年之后的时间
     *
     * @return
     */
    public static long getAfterYearTime(int year) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, year);
        return cal.getTimeInMillis();
    }

    /**
     * 检查时间是否到期
     *
     * @param date
     * @return
     */
    public static boolean isOutOfDate(Date date) {
        if (date != null) {
            return date.getTime() <= new Date().getTime();
        }
        return false;
    }

    /**
     * 获取新的期数
     *
     * @param period
     * @return
     */
    public static String getNewPeriod(String appId, String period) throws ParseException {
        String newPeriod = "";
        switch (appId) {
            case "7":
                newPeriod = String.valueOf(Long.parseLong(period) + 1);
                break;
            case "8":
                newPeriod = getNextPeriod(period, 180);
                break;
            case "9":
                newPeriod = getNextPeriod(period, 120);
                break;
            case "10":
                newPeriod = getNextPeriod(period, 120);
                break;
        }
        return newPeriod;
    }

    private static String getNextPeriod(String period, int periodCount) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date date = sdf.parse(period.substring(0, 8));
        Calendar cal = Calendar.getInstance();//得到当前时间
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return Integer.parseInt(period.substring(8)) == periodCount ? sdf.format(cal.getTime()) + "001" : (Long.parseLong(period) + 1) + "";
    }
}
